package cn.e4j.core.util;

import cn.e4j.core.cache.CacheKey;
import cn.e4j.core.cache.CacheUtil;
import cn.e4j.modular.system.entity.SysParams;
import cn.e4j.modular.system.entity.SysUser;
import cn.e4j.modular.system.mapper.SysParamsMapper;
import cn.e4j.modular.system.mapper.SysUserMapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.toolkit.SqlRunner;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <b>应用系统上下文</b>
 *
 * @author wenchuan.li
 */
@SuppressWarnings("all")
public class AppCxt {


    /**
     * 根据当前线程id存放 用户信息
     */
    public static HashMap<Long, SysUser> currentThreadUser = new HashMap<Long, SysUser>();

    /**
     * 从缓存中获取全局参数配置值
     *
     * @param key
     * @return
     */
    public static String getParam(String key) {
        if (ToolUtil.isEmpty(key)) {
            return null;
        }
        String _key = CacheKey.PREFIX + CacheKey.G_PARAM_ + key;
        String keyValue = CacheUtil.get(CacheUtil.CONSTANT, _key);
        if (keyValue == null) {
            SysParamsMapper sysParamsMapper = SpringContextHolder.getBean(SysParamsMapper.class);
            SysParams sysParams = sysParamsMapper.getByKey(key);
            //TODO sqlRunner 导致项目死掉
            //Map<String, Object> map = SqlRunner.db().selectOne("select VALUE from SYS_PARAMS where CODE='" + key + "'");
            if (ToolUtil.isNotEmpty(sysParams) && ToolUtil.isNotEmpty(sysParams.getVALUE())) {
                keyValue = sysParams.getVALUE();
                CacheUtil.put(CacheUtil.CONSTANT, _key, keyValue);
            }
        }
        return keyValue;
    }

    /**
     * 从缓存中 移除全局参数配置值
     *
     * @param key
     * @return
     */
    public static void delParam(String key) {
        if (!ToolUtil.isEmpty(key)) {
            CacheUtil.remove(CacheUtil.CONSTANT, CacheKey.PREFIX + CacheKey.G_PARAM_ + key);
        }
    }


    /**
     * 从缓存中获取用户信息
     *
     * @param key
     * @return
     */
    public static SysUser getUserInfoCache(String id) {
        if (ToolUtil.isEmpty(id)) {
            return null;
        }
        String _key = CacheKey.PREFIX + CacheKey.G_LOGIN_USER + id;
        SysUser keyValue = CacheUtil.get(CacheUtil.TEMP, _key);
        if (keyValue == null) {
            SysUserMapper sysUserMapper = SpringContextHolder.getBean("sysUserMapper");
            List<SysUser> sysUserList = sysUserMapper.selectList(new QueryWrapper<SysUser>()
                    .lambda()
                    .eq(SysUser::getID, id)
            );
            if (sysUserList.size() > 0) {
                keyValue = sysUserList.get(0);
                CacheUtil.put(CacheUtil.TEMP, _key, keyValue);
            }
            //Map<String, Object> map = SqlRunner.db().selectOne("select * from SYS_USER where IS_DEL='0' and ID='" + id + "'");
//            if (ToolUtil.isNotEmpty(map)) {
//                CacheUtil.put(CacheUtil.TEMP, _key, map);
//            }
        }
        return keyValue;
    }

    /**
     * 用户信息添加到缓存
     *
     * @param key
     * @return
     */
    public static void addUserInfoCache(SysUser sysUser) {
        if (!ToolUtil.isEmpty(sysUser)) {
            CacheUtil.put(CacheUtil.TEMP,
                    CacheKey.PREFIX + CacheKey.G_LOGIN_USER + sysUser.getID(),
                    sysUser
            );
        }
    }

    /**
     * 从缓存中 移除用户信息
     *
     * @param key
     * @return
     */
    public static void delUserInfoCache(String id) {
        if (!ToolUtil.isEmpty(id)) {
            CacheUtil.remove(CacheUtil.TEMP, CacheKey.PREFIX + CacheKey.G_LOGIN_USER + id);
        }
    }

    /**
     * 生成表id值公共方法-根据当前时间 毫秒
     */
    private static long _id = 0;

    public synchronized static String timeId() {
        long time = System.currentTimeMillis();
        if (time > _id) {
            _id = time;
        } else {
            _id += 1;
        }
        return _id + "";
    }


    public static void addCurrentThreadUser(Long id, SysUser user){
        currentThreadUser.put(id, user);
    }

    public static SysUser getCurrentThreadUser(Long id){
        return currentThreadUser.get(id);
    }

    public static void removeCurrentThreadUser(Long id){
        currentThreadUser.remove(id);
    }

}
